var bilibili;
(() => {
  var t = {
      2773: (t, e, i) => {
        var n = i(4606);
        t.exports = n;
      },
      3893: (t, e, i) => {
        var n =
            /(<\/?[a-z][a-z0-9]*(?::[a-z][a-z0-9]*)?\s*(?:\s+[a-z0-9-_]+=(?:(?:'[\s\S]*?')|(?:"[\s\S]*?")))*\s*\/?>)|([^<]|<(?![a-z\/]))*/gi,
          r = /\s[a-z0-9-_]+\b(\s*=\s*('|")[\s\S]*?\2)?/gi,
          o = /(\s[a-z0-9-_]+\b\s*)(?:=(\s*('|")[\s\S]*?\3))?/gi,
          a = /^<[a-z]/,
          s = /\/>$/,
          l = /^<\//,
          c = /<\/?([a-z][a-z0-9]*)(?::([a-z][a-z0-9]*))?/i,
          u = /^('|")|('|")$/g,
          d =
            /^(?:area|base|br|col|command|embed|hr|img|input|link|meta|param|source)/i,
          p = i(6917);
        function h(t, e, i) {
          for (
            var h,
              f,
              m,
              g,
              y,
              b,
              v,
              w,
              x = [],
              N = 0,
              _ = t.match(n),
              S = !1,
              T = null,
              O = 0,
              E = _.length;
            O < E;
            O++
          )
            if (
              ((f = (w = _[O]).match(c)), (h = e.test(w)) && !S && (S = !0), S)
            ) {
              if (a.test(w)) {
                (m = s.test(w) || d.test(f[1])), (g = []);
                for (var $ = 0, C = (b = w.match(r) || []).length; $ < C; $++)
                  (o.lastIndex = 0),
                    (y = o.exec(b[$])),
                    g.push({
                      name: y[1].trim(),
                      value: (y[2] || "").trim().replace(u, ""),
                    });
                ((T && T.childNodes) || x).push(
                  (v = new p({
                    nodeType: 1,
                    nodeName: f[1],
                    namespace: f[2],
                    attributes: g,
                    childNodes: [],
                    parentNode: T,
                    startTag: w,
                    selfCloseTag: m,
                  }))
                ),
                  N++,
                  !i && h && T && x.push(v),
                  m ? N-- : (T = v);
              } else
                l.test(w)
                  ? T.nodeName == f[1] && ((T = T.parentNode), N--)
                  : T.childNodes.push(
                      new p({ nodeType: 3, text: w, parentNode: T })
                    );
              if (0 == N && ((S = !1), (T = null), i)) break;
            }
          return i ? x[0] || null : x;
        }
        function f(t) {
          this.rawHTML = t;
        }
        (f.prototype.getElementsByClassName = function (t) {
          var e = new RegExp("class=('|\")(.*?\\s)?" + t + "(\\s.*?)?\\1");
          return h(this.rawHTML, e);
        }),
          (f.prototype.getElementsByTagName = function (t) {
            var e = new RegExp("^<" + t, "i");
            return h(this.rawHTML, e);
          }),
          (f.prototype.getElementById = function (t) {
            var e = new RegExp("id=('|\")" + t + "\\1");
            return h(this.rawHTML, e, !0);
          }),
          (f.prototype.getElementsByName = function (t) {
            return this.getElementsByAttribute("name", t);
          }),
          (f.prototype.getElementsByAttribute = function (t, e) {
            var i = new RegExp("\\s" + t + "=('|\")" + e + "\\1");
            return h(this.rawHTML, i);
          }),
          (t.exports = f);
      },
      4606: (t, e, i) => {
        var n = i(3893);
        function r() {}
        (r.prototype.parseFromString = function (t) {
          return new n(t);
        }),
          (t.exports = r);
      },
      6917: (t) => {
        function e(t) {
          (this.namespace = t.namespace || null),
            (this.text = t.text),
            (this._selfCloseTag = t.selfCloseTag),
            Object.defineProperties(this, {
              nodeType: { value: t.nodeType },
              nodeName: { value: 1 == t.nodeType ? t.nodeName : "#text" },
              childNodes: { value: t.childNodes },
              firstChild: {
                get: function () {
                  return this.childNodes[0] || null;
                },
              },
              lastChild: {
                get: function () {
                  return this.childNodes[this.childNodes.length - 1] || null;
                },
              },
              parentNode: { value: t.parentNode || null },
              attributes: { value: t.attributes || [] },
              innerHTML: {
                get: function () {
                  for (
                    var t, e = "", i = 0, n = this.childNodes.length;
                    i < n;
                    i++
                  )
                    e +=
                      3 === (t = this.childNodes[i]).nodeType
                        ? t.text
                        : t.outerHTML;
                  return e;
                },
              },
              outerHTML: {
                get: function () {
                  if (3 != this.nodeType) {
                    var t,
                      e = (
                        this.attributes.map(function (t) {
                          return t.name + (t.value ? '="' + t.value + '"' : "");
                        }) || []
                      ).join(" ");
                    (t =
                      "<" +
                      this.nodeName +
                      (e ? " " + e : "") +
                      (this._selfCloseTag ? "/" : "") +
                      ">"),
                      this._selfCloseTag ||
                        ((t += (
                          this._selfCloseTag
                            ? ""
                            : this.childNodes.map(function (t) {
                                return t.outerHTML;
                              }) || []
                        ).join("")),
                        (t += "</" + this.nodeName + ">"));
                  } else t = this.textContent;
                  return t;
                },
              },
              textContent: {
                get: function () {
                  return this.nodeType == e.TEXT_NODE
                    ? this.text
                    : this.childNodes
                        .map(function (t) {
                          return t.textContent;
                        })
                        .join("")
                        .replace(/\x20+/g, " ");
                },
              },
            });
        }
        function i(t, e, n) {
          var r = [];
          if (((n = !!n), 3 !== t.nodeType))
            for (
              var o = 0, a = t.childNodes.length;
              o < a &&
              (3 === t.childNodes[o].nodeType ||
                !e(t.childNodes[o]) ||
                (r.push(t.childNodes[o]), !n));
              o++
            )
              r = r.concat(i(t.childNodes[o], e));
          return n ? r[0] : r;
        }
        (e.prototype.getAttribute = function (t) {
          for (var e = 0, i = this.attributes.length; e < i; e++)
            if (this.attributes[e].name == t) return this.attributes[e].value;
          return null;
        }),
          (e.prototype.getElementsByTagName = function (t) {
            return i(this, function (e) {
              return e.nodeName == t;
            });
          }),
          (e.prototype.getElementsByClassName = function (t) {
            var e = new RegExp("^(.*?\\s)?" + t + "(\\s.*?)?$");
            return i(this, function (t) {
              return t.attributes.length && e.test(t.getAttribute("class"));
            });
          }),
          (e.prototype.getElementById = function (t) {
            return i(
              this,
              function (e) {
                return e.attributes.length && e.getAttribute("id") == t;
              },
              !0
            );
          }),
          (e.prototype.getElementsByName = function (t) {
            return i(this, function (e) {
              return e.attributes.length && e.getAttribute("name") == t;
            });
          }),
          (e.ELEMENT_NODE = 1),
          (e.TEXT_NODE = 3),
          (t.exports = e);
      },
    },
    e = {};
  function i(n) {
    var r = e[n];
    if (void 0 !== r) return r.exports;
    var o = (e[n] = { exports: {} });
    return t[n](o, o.exports, i), o.exports;
  }
  (i.n = (t) => {
    var e = t && t.__esModule ? () => t.default : () => t;
    return i.d(e, { a: e }), e;
  }),
    (i.d = (t, e) => {
      for (var n in e)
        i.o(e, n) &&
          !i.o(t, n) &&
          Object.defineProperty(t, n, { enumerable: !0, get: e[n] });
    }),
    (i.o = (t, e) => Object.prototype.hasOwnProperty.call(t, e)),
    (i.r = (t) => {
      "undefined" != typeof Symbol &&
        Symbol.toStringTag &&
        Object.defineProperty(t, Symbol.toStringTag, { value: "Module" }),
        Object.defineProperty(t, "__esModule", { value: !0 });
    });
  var n = {};
  (() => {
    "use strict";
    i.r(n),
      i.d(n, {
        getAlbum: () => x,
        getArtist: () => N,
        getPlaylist: () => S,
        getTrack: () => _,
        hitPlaylist: () => b,
        hitPlaylistDetail: () => v,
        search: () => w,
      });
    var t = i(2773),
      e = function (t, e, i, n) {
        return new (i || (i = Promise))(function (r, o) {
          function a(t) {
            try {
              l(n.next(t));
            } catch (t) {
              o(t);
            }
          }
          function s(t) {
            try {
              l(n.throw(t));
            } catch (t) {
              o(t);
            }
          }
          function l(t) {
            var e;
            t.done
              ? r(t.value)
              : ((e = t.value),
                e instanceof i
                  ? e
                  : new i(function (t) {
                      t(e);
                    })).then(a, s);
          }
          l((n = n.apply(t, e || [])).next());
        });
      };
    const r =
        /[\u0000-\u001F]|[\u007F-\u009F]|[\u2000-\u20FF]|[\uD800-\uDFFF]/g,
      o = /<[^>]*>/g,
      a = /[\r|\n|\t|\\]/g;
    function s(t) {
      return e(this, void 0, void 0, function* () {
        const e = (yield t.text()).replace(r, "").replace(o, "").replace(a, "");
        return JSON.parse(e);
      });
    }
    const l = new (i.n(t)())();
    function c(t, e = "body") {
      return l.parseFromString(t).getElementsByTagName(e).textContent;
    }
    const u = Object.prototype.toString,
      d = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
    function p(t) {
      return -1 !== t.indexOf("%") ? decodeURIComponent(t) : t;
    }
    function h(t) {
      return encodeURIComponent(t);
    }
    function f(t, e) {
      try {
        return e(t);
      } catch (e) {
        return t;
      }
    }
    class m {
      constructor(t, e, i, n, r, o, a, s, l) {
        (this.maxAge = t),
          (this.domain = e),
          (this.path = i),
          (this.expires = n),
          (this.httpOnly = r),
          (this.partitioned = o),
          (this.priority = a),
          (this.sameSite = s),
          (this.secure = l);
      }
      static from(t) {
        const e = new m(
          t.maxAge,
          t.domain,
          t.path,
          t.expires,
          t.httpOnly,
          t.partitioned,
          t.priority,
          t.sameSite,
          t.secure
        );
        return (e.name = t.name), (e.value = t.value), e;
      }
      parse(t, e) {
        let i = e || p,
          n = 0;
        for (; n < t.length; ) {
          let e = t.indexOf("=", n);
          if (-1 === e) break;
          let r = t.indexOf(";", n);
          if (-1 === r) r = t.length;
          else if (r < e) {
            n = t.lastIndexOf(";", e - 1) + 1;
            continue;
          }
          let o = t.slice(n, e).trim();
          if (void 0 === this[o]) {
            let n = t.slice(e + 1, r).trim();
            34 === n.charCodeAt(0) && (n = n.slice(1, -1)), (this[o] = f(n, i));
          }
          n = r + 1;
        }
        return this;
      }
      stringify(t, e, i) {
        let n = i || {},
          r = n.encode || h;
        if (!d.test(t)) throw new TypeError("argument name is invalid");
        let o = r(e);
        if (o && !d.test(o)) throw new TypeError("argument val is invalid");
        let a = t + "=" + o;
        if (null != n.maxAge) {
          let t = n.maxAge - 0;
          if (isNaN(t) || !isFinite(t))
            throw new TypeError("option maxAge is invalid");
          a += "; Max-Age=" + Math.floor(t);
        }
        if (n.domain) {
          if (!d.test(n.domain))
            throw new TypeError("option domain is invalid");
          a += "; Domain=" + n.domain;
        }
        if (n.path) {
          if (!d.test(n.path)) throw new TypeError("option path is invalid");
          a += "; Path=" + n.path;
        }
        if (n.expires) {
          var s = n.expires;
          if (
            !(function (t) {
              return "[object Date]" === u.call(t) || t instanceof Date;
            })(s) ||
            isNaN(s.valueOf())
          )
            throw new TypeError("option expires is invalid");
          a += "; Expires=" + s.toUTCString();
        }
        return (
          n.httpOnly && (a += "; HttpOnly"),
          n.secure && (a += "; Secure"),
          n.partitioned && (a += "; Partitioned"),
          n.priority && (a += `; Priority=${n.priority}`),
          n.sameSite && (a += `; SameSite=${n.sameSite}`),
          a
        );
      }
      equal(t) {
        if ((this.path && !t.path) || (this.domain && !t.domain)) return !1;
        if (this.path && t.path && 0 !== t.path.indexOf(this.path)) return !1;
        const e = t.domain && t.domain.replace(/^[\.]/, "");
        return (this.domain && this.domain.replace(/^[\.]/, "")) == e;
      }
    }
    const g = new (class {
      constructor() {
        this.cookieMap = {};
      }
      get(t, e) {
        let i = null;
        const n = this.cookieMap[t];
        if (!n || !n.length) return null;
        for (let r = 0; r < n.length; r++)
          if (((i = n[r]), i.expires && i.expires.getDate() <= Date.now())) {
            if (i.equal(m.from(e))) break;
            n.length || delete this.cookieMap[t];
          }
        return null !== i && i.equal(m.from(e)) ? i : null;
      }
      set(t, e) {
        let i = !1;
        e.expires && e.expires.getMilliseconds() <= Date.now() && (i = !0);
        const n = new m(
          e.maxAge,
          e.domain,
          e.path,
          e.expires,
          e.httpOnly,
          e.partitioned,
          e.priority,
          e.sameSite,
          e.secure
        );
        if (this.cookieMap[t]) {
          const e = this.cookieMap[t];
          for (let r = 0; r < e.length; r++)
            if (e[r].equal(n))
              return i
                ? (e.splice(r, 1), e.length || delete this.cookieMap[t], !1)
                : ((e[r] = n), !0);
        }
        return !i && ((this.cookieMap[t] = [n]), !0);
      }
      remove(t, e) {
        if (!e) return delete this.cookieMap[t], !0;
        if (!(null == e ? void 0 : e.name)) return !1;
        const i = this.cookieMap[t],
          n = i.findIndex((t) => t.name == e.name);
        return -1 != n && (i.splice(n, 1), !0);
      }
    })();
    var y = function (t, e, i, n) {
      return new (i || (i = Promise))(function (r, o) {
        function a(t) {
          try {
            l(n.next(t));
          } catch (t) {
            o(t);
          }
        }
        function s(t) {
          try {
            l(n.throw(t));
          } catch (t) {
            o(t);
          }
        }
        function l(t) {
          var e;
          t.done
            ? r(t.value)
            : ((e = t.value),
              e instanceof i
                ? e
                : new i(function (t) {
                    t(e);
                  })).then(a, s);
        }
        l((n = n.apply(t, e || [])).next());
      });
    };
    function b(t) {
      return y(this, void 0, void 0, function* () {
        try {
          void 0 === t && (t = 0);
          const e =
            "https://www.bilibili.com/audio/music-service-c/web/menu/hit?ps=20&pn=" +
            (t / 20 + 1);
          let i = yield fetch(e, {
            // headers: { Accept: "application/json, text/plain, */*" },
          });
          const n = (yield s(i)).data.data.map((t) => ({
            picUrl: t.cover,
            name: t.title,
            introduction: t.intro,
            id: t.menuId,
            source_url: `https://www.bilibili.com/audio/am${t.menuId}`,
          }));
          return JSON.stringify(n);
        } catch (t) {
          return JSON.stringify(`[js-bili-hitPlaylist] ${t.message}`);
        }
      });
    }
    function v(t) {
      return y(this, void 0, void 0, function* () {
        try {
          const e = `https://www.bilibili.com/audio/music-service-c/web/url?sid=${t}`;
          let i = yield fetch(e, {
            headers: { Accept: "application/json, text/plain, */*" },
          });
          const n = yield s(i);
          console.log(`[js-bili-hitPlaylistDetail] ${n}`);
          const r = n.data,
            o = {
              id: r.sid,
              info: r.info,
              timeout: r.timeout,
              size: r.size,
              sources: r.cdns,
              qualities: r.qualities,
              title: r.title,
              cover: r.cover,
            };
          return JSON.stringify(o);
        } catch (t) {
          return JSON.stringify(`[js-bili-hitPlaylistDetail] ${t.message}`);
        }
      });
    }
    function w(t, e) {
      return y(this, void 0, void 0, function* () {
        try {
          const e = `https://api.bilibili.com/x/web-interface/search/type?__refresh__=true&_extra=&context=&page=1&page_size=42&platform=pc&highlight=1&single_column=0&keyword=${encodeURIComponent(
              t
            )}&category_id=&search_type=video&dynamic_offset=0&preload=true&com2co=true`,
            i = "https://api.bilibili.com",
            n = "buvid3",
            r = new Date(new Date().setFullYear(new Date().getFullYear() + 1)),
            o = g.get(i, { name: n });
          o || g.set(i, { name: n, value: "0", expires: r }),
            null == o ? void 0 : o.value;
          const a = yield fetch(e, {
              headers: {
                Accept: "application/json, text/plain, */*",
                "Accept-Encoding": "gzip, deflate, br",
                "Accept-Language":
                  "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
                "Cache-Control": "no-cache",
                Cookie: "buvid3=02C861B6-C115-5321-CB75-C05D40DA853724734infoc",
                "User-Agent":
                  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.0.0",
                "Sec-Fetch-Site": "none",
                "Sec-Fetch-Mode": "cors",
                "Sec-Fetch-Dest": "empty",
                "Sec-Ch-Ua-Platform": "macOS",
                "Sec-Ch-Ua-Mobile": "?0",
                "Sec-Ch-Ua":
                  '"Microsoft Edge";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
                Referer: "https://www.bilibili.com/",
                Pragma: "no-cache",
              },
            }),
            l = yield s(a),
            c = l.data.result.map((t) => T(t)),
            u = l.data.numResults;
          return JSON.stringify({ result: c, total: u });
        } catch (t) {
          return JSON.stringify(`[js-bili-search] ${t.message} ${t.stack}\n`);
        }
      });
    }
    function x(t) {
      return y(this, void 0, void 0, function* () {});
    }
    function N(t) {
      return y(this, void 0, void 0, function* () {
        try {
          let e = `https://api.bilibili.com/x/space/acc/info?mid=${t}&jsonp=jsonp`,
            i = yield fetch(e),
            n = yield s(i);
          const r = {
            conver_img_url: n.data.face,
            title: n.data.name,
            id: t,
            source_url: `https://space.bilibili.com/${t}/#/audio`,
          };
          if (3 === t.length) {
            (e = `https://api.bilibili.com/x/space/arc/search?mid=${t}&pn=1&ps=25&order=click&index=1&jsonp=jsonp`),
              (i = yield fetch(e)),
              (n = yield s(i));
            const o = n.data.list.vlist.map((t) => T(t));
            return JSON.stringify({ info: r, tracks: o });
          }
          (e = `https://api.bilibili.com/audio/music-service-c/web/song/upper?pn=1&ps=0&order=2&uid=${t}`),
            (i = yield fetch(e)),
            (n = yield s(i));
          const o = n.data.data.map((t) => ({
            id: t.id,
            title: t.title,
            artist: t.uname,
            artist_id: `biliartist_${t.uid}`,
            source: "bilibili",
            source_url: `https://www.bilibili.com/audio/au${t.id}`,
            img_url: t.cover,
            lyric_url: t.lyric,
          }));
          return JSON.stringify({ info: r, tracks: o });
        } catch (t) {
          return JSON.stringify(`[js-bili-getArtist] ${t.message}`);
        }
      });
    }
    function _(t) {
      return y(this, void 0, void 0, function* () {
        var e;
        try {
          const i = `https://api.bilibili.com/x/web-interface/view?bvid=${t}`;
          let n = yield fetch(i),
            r = yield s(n);
          const o = {
              cover_img_url: r.data.pic,
              title: r.data.title,
              id: t,
              source_url: `https://www.bilibili.com/${t}`,
              author: r.data.owner,
            },
            a =
              (r.data.owner,
              r.data.pic,
              null !== (e = r.data.pages[0]) && void 0 !== e ? e : t),
            l = `http://api.bilibili.com/x/player/playurl?fnval=16&bvid=${t}&cid=${a}`;
          if (
            ((n = yield fetch(l)),
            (r = yield s(n)),
            r.data.data.dash.audio.length)
          )
            return (
              Object.defineProperty(
                o,
                "url",
                r.data.data.dash.audio[0].baseUrl
              ),
              JSON.stringify(o)
            );
          {
            const e = `https://www.bilibili.com/audio/music-service-c/web/url?sid=${t}`;
            if (((n = yield fetch(e)), (r = yield s(n)), 0 === r.data.code))
              return (
                Object.defineProperty(o, "url", r.data.data.cdns),
                JSON.stringify(o)
              );
            throw Error("[getTrack] not found.");
          }
        } catch (t) {
          return JSON.stringify(`[js-bili-getTrack] ${t.message}`);
        }
      });
    }
    function S(t) {
      return y(this, void 0, void 0, function* () {
        try {
          let e = `https://www.bilibili.com/audio/music-service-c/web/menu/info?sid=${t}`,
            i = yield fetch(e),
            n = yield s(i);
          console.log("[js-bili-getPlaylist]1", e, n);
          const r = {
            cover_img_url: n.data.conver,
            title: n.data.title,
            id: t,
            source_url: `https://www.bilibili.com/audio/am${t}`,
          };
          (e = `https://www.bilibili.com/audio/music-service-c/web/song/of-menu?pn=1&ps=100&sid=${t}`),
            (i = yield fetch(e)),
            (n = yield s(i));
          const o = n.data.data.map((t) => ({
            id: t.id,
            title: t.title,
            artist: t.uname,
            artist_id: t.uid,
            intro: t.intro,
            duration: t.duration,
            source: "bilibili",
            source_url: `https://www.bilibili.com/audio/au${t.id}`,
            img_url: t.cover,
            lyric_url: t.lyric,
          }));
          return JSON.stringify({ info: r, tracks: o });
        } catch (t) {
          return JSON.stringify(`[js-bili-getPlaylist] ${t.message}`);
        }
      });
    }
    function T(t) {
      let e = t.pic;
      return (
        e.startsWith("//") && (e = `https://${e}`),
        {
          id: t.bvid,
          title: c(t.title),
          artist: c(t.author),
          artist_id: `biliartist_v_${t.mid}`,
          source_url: `https://www.bilibili.com/${t.bvid}`,
          img_url: e,
        }
      );
    }
  })(),
    (bilibili = n);
})();
